package com.entity.implement;

import java.util.Date;
import java.util.List;

import com.entity.myInterface.UserDaoCustom;
import com.okok.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;


@Repository
public class UserDaoImpl implements UserDaoCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public User findUserByLoginNameAndDelflag(String loginName, String app) {
        Criteria criteria = Criteria.where("delflag").is(false);
        criteria.and("loginName").is(loginName);

        if (StringUtils.isNotBlank(app)) {
            criteria.and("app").is(app);
        }

        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, User.class);
    }

    @Override
    public User findUserByEmail(String email,String app){
        Criteria criteria = Criteria.where("delfalg").is(false);
        criteria.and("email").is(email);

        if(StringUtils.isNotBlank(app)){
            criteria.and("app").is(app);
        }
        Query query = new Query(criteria);

        return mongoTemplate.findOne(query, User.class);

    }


    @Override
    public List<User> findUserByDate(Date date) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void resetPass(User user, String op, String newpass) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is( user.getId() ) );

        Update update = new Update();
        update.set("loginPwd", newpass);
        update.set("modifier", op);
        update.set("modifyDate", new Date() );

        mongoTemplate.updateFirst(query, update, User.class);
    }
}
